﻿using System;
using System.Collections.Generic;
using System.Text;
using MapCenterSystem.DataAccess;
using System.Drawing;
using MapCenterSystem.DataInfo;
using System.IO;
namespace MapCenterSystem.ServiceProvider.LocalMap
{
    public class LocalMapProvider : MapProviderService
    {
        private Image[,] image = new Image[3, 3];

        //private Point ConvertToLocalTile(Point  standardTile)
        //{
        //    Point local = new Point();
        //    local.X = (standardTile.X - Vertical-1) * 256;
        //    local.Y = (standardTile.Y - Horizontal) * 256;
        //    return local;
        //}
        private byte[] GetLocalMap(MapInfo map)
        {
            if(!map.Exist())
            {
                Bitmap noImage= new Bitmap(Path.Combine(map.MapStore,"noimage.map"));
                byte[] result = map.ConvertToByte(noImage);
                noImage.Dispose();
                return result;
            }
            return map.ConvertToByte();
        }

        private void InitListImage(MapInfo map)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Point currentTile = new Point(map.Tile.X +i, map.Tile.Y +j);
                    MapInfo mapTemp = new MapInfo();
                    mapTemp.EarthPoint = map.EarthPoint;
                    mapTemp.MapStore = map.MapStore;
                    mapTemp.Size = map.Size;
                    mapTemp.Source = map.Source;
                    mapTemp.Zoom = map.Zoom;
                    mapTemp.Tile = currentTile;
                    Image imageMap = map.GetMapFromByte(GetLocalMap(mapTemp));
                    image[i, j] = new Bitmap(imageMap);
                    imageMap.Dispose();
                }
            }
        }

        //Ghep hinh 
        private Image CombineImage()
        {
            Image memImage = new Bitmap(3 * 256, 3 * 256);
            Graphics g = Graphics.FromImage(memImage);
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    g.DrawImage(image[i, j], i * 256, j * 256);
                }
            }
            g.Dispose();
            return memImage;
        }

        //Lay hinh ra
        public override byte[] GetMap(MapInfo map)
        {
            map.MapStore = Path.Combine(map.MapStore, "MobiLocate");
            map.Tile = map.GetTileFromEarthPoint();
           // Point tile = ConvertToLocalTile(map.GetTileFromEarthPoint());
            Point startPoint = map.TileStartPoint;
            Point distantTwoPoint = new Point();
            distantTwoPoint.X = Math.Abs(map.EarthPoint.X - startPoint.X);
            distantTwoPoint.Y = Math.Abs(map.EarthPoint.Y - startPoint.Y);
            InitListImage(map);
            Image memImage = CombineImage();
            Image currentImage = new Bitmap(map.Size.Width, map.Size.Height);
            Graphics g = Graphics.FromImage(currentImage);
            g.DrawImage(memImage, new Rectangle(0, 0, currentImage.Width, currentImage.Height),
                new Rectangle(distantTwoPoint.X, distantTwoPoint.Y, currentImage.Width, currentImage.Height), GraphicsUnit.Pixel);
            g.Dispose();
            return map.ConvertToByte(currentImage);
        }

        public override void Dispose()
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    image[i, j].Dispose();
                }
            }
        }

        
    }
}
